package com.xjiye_test.payServer.servletGroup.weifutong;

import com.xjiye_test.payServer.serverletUtil.ReadBytes;
import com.xjiye_test.payServer.serverletUtil.Signature;
import com.xjiye_test.payServer.serverletUtil.XMLParser;
import org.xml.sax.SAXException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/10.
 */
public class gateway extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        String reStr=null;
        req.setCharacterEncoding("UTF-8");
        int size = req.getContentLength();
        InputStream is = req.getInputStream();
        reStr=new String(ReadBytes.readBytes(is, size));
        Map map = null;
        try {
            map = XMLParser.getMapFromXML(reStr);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        if("unified.trade.micropay".equals(map.get("service"))){
            out.print(micropay());
            out.flush();
        }
        if("unified.trade.query".equals(map.get("service"))){
            out.print(query());
            out.flush();
        }
        if("unified.micropay.reverse".equals(map.get("service"))){
            out.print(reverse());
            out.flush();
        }
    }

    private String micropay(){
        System.out.println("==========================发起支付====================");
        String re="<xml><appid><![CDATA[2016092301958221]]></appid>\n" +
                "<charset><![CDATA[UTF-8]]></charset>\n" +
                "<code><![CDATA[10003]]></code>\n" +
                "<err_code><![CDATA[10003]]></err_code>\n" +
                "<err_msg><![CDATA[付款码无效或等待用户输入密码]]></err_msg>\n" +
                "<mch_id><![CDATA[102511312601]]></mch_id>\n" +
                "<need_query><![CDATA[Y]]></need_query>\n" +
                "<nonce_str><![CDATA[1EQjbmbac3AGR607dkUWSLQ4nREh4hmG]]></nonce_str>\n" +
                "<result_code><![CDATA[1]]></result_code>\n" +
                "<sign_type><![CDATA[MD5]]></sign_type>\n" +
                "<status><![CDATA[0]]></status>\n" +
                "<version><![CDATA[2.0]]></version>\n" +
                "</xml>\n";
        String reString=null;
        try {
            reString=Signature.getSignFromResponseString(re,"7edb3f12704c20999479c00741187103");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }finally {
            return  reString;
        }
    }

    private String query(){
        System.out.println("=============查询订单====================="+msgStatus.getStep()+"次");
        String re="<xml><appid><![CDATA[2016092301958221]]></appid>\n" +
                "<charset><![CDATA[UTF-8]]></charset>\n" +
                "<mch_id><![CDATA[102511312601]]></mch_id>\n" +
                "<nonce_str><![CDATA[pNzBvbcRgpHRjed5khj1JBH5Dy83Pjfn]]></nonce_str>\n" +
                "<result_code><![CDATA[0]]></result_code>\n" +
                "<sign_type><![CDATA[MD5]]></sign_type>\n" +
                "<status><![CDATA[0]]></status>\n" +
                "<trade_state><![CDATA[NOTPAY]]></trade_state>\n" +
                "<version><![CDATA[2.0]]></version>\n" +
                "</xml>";

        if(msgStatus.getStep()==msgStatus.getSucsscC()){

            msgStatus.setStep(0);
        }
        if(msgStatus.getStep()==msgStatus.getNeed_queryC()){
            re="<xml><appid><![CDATA[2016092301958221]]></appid>\n" +
                    "<charset><![CDATA[UTF-8]]></charset>\n" +
                    "<mch_id><![CDATA[102511312601]]></mch_id>\n" +
                    "<nonce_str><![CDATA[pNzBvbcRgpHRjed5khj1JBH5Dy83Pjfn]]></nonce_str>\n" +
                    "<result_code><![CDATA[1]]></result_code>\n" +
                    "<need_query><![CDATA["+msgStatus.getNeed_query()+"]]></need_query>\n" +
                    "<sign_type><![CDATA[MD5]]></sign_type>\n" +
                    "<status><![CDATA[0]]></status>\n" +
                    "<trade_state><![CDATA[NOTPAY]]></trade_state>\n" +
                    "<version><![CDATA[2.0]]></version>\n" +
                    "</xml>";
            msgStatus.setStep(msgStatus.getStep()+1);
        }
        String reString=null;
        try {
            reString=Signature.getSignFromResponseString(re,"7edb3f12704c20999479c00741187103");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }finally {
            return  reString;
        }
    }

    private String reverse(){
        System.out.println("=============撤销订单=====================");
        String re="<xml><action><![CDATA[close]]></action>\n" +
                "<charset><![CDATA[UTF-8]]></charset>\n" +
                "<mch_id><![CDATA[102511312601]]></mch_id>\n" +
                "<nonce_str><![CDATA[GS11ug7zXRjFuQJ11TETynCgHi96sHVa]]></nonce_str>\n" +
                "<out_trade_no><![CDATA[P1707101141280010001]]></out_trade_no>\n" +
                "<result_code><![CDATA[0]]></result_code>\n" +
                "<retry_flag><![CDATA[N]]></retry_flag>\n" +
                "<sign_type><![CDATA[MD5]]></sign_type>\n" +
                "<status><![CDATA[0]]></status>\n" +
                "<trade_type><![CDATA[pay.alipay.micropay]]></trade_type>\n" +
                "<transaction_id><![CDATA[102511312601201707107237345194]]></transaction_id>\n" +
                "<version><![CDATA[2.0]]></version>\n" +
                "</xml>";
        String reString=null;
        try {
            reString=Signature.getSignFromResponseString(re,"7edb3f12704c20999479c00741187103");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }finally {
            return  reString;
        }
    }
}
